package com.wing.res.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.common.exception.BusinessException;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.res.model.entity.ReceiptRecord;
import com.wing.res.service.ReceiptRecordService;
import com.wing.web.db.config.util.DynamicDataSource;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 领料记录-controller
 *
 * @author xiayang
 */
@Validated
@Api(value = "领料记录管理", tags = "#/res/receiptRecord/receiptRecord")
@RestController("receiptRecordController")
@RequestMapping("receiptRecord/admin/receiptRecord")
@RequiredArgsConstructor
public class ReceiptRecordController {

    private final ReceiptRecordService receiptRecordService;

    @ApiOperation(value = "查询领料记录列表", notes = "搜索")
    @GetMapping("/list")
    public PageResult<ReceiptRecord> list(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer limit,
            @RequestParam(name = "workOrderCode", required = false) String workOrderCode,
            @RequestParam(name = "planId", required = false) Long planId,
            @RequestParam(name = "processId", required = false) Long processId,
            Long inventoryTypeId,
            @RequestParam(name = "inventoryTypeCode", required = false) String inventoryTypeCode,
            @RequestParam(name = "productionPlanCode", required = false) String productionPlanCode,
            @RequestParam(name = "productionRecordCode", required = false) String productionRecordCode,
            @RequestParam(name = "skuCode", required = false) String skuCode,
            @RequestParam(name = "status", required = false) String status,
            @RequestParam(name = "workShopId", required = false) String workShopId,
            @RequestParam(name = "isNotNeedOk", required = false) String isNotNeedOk,
            @RequestParam(name = "flagStatus", required = false) String flagStatus
            , String startCreateTime
            , String endCreateTime
    ) {
        Page<ReceiptRecord> pg = new Page<>(page, limit);
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("workOrderCode", workOrderCode);
        params.put("planId", planId);
        params.put("processId", processId);
        params.put("inventoryTypeId", inventoryTypeId);
        params.put("inventoryTypeCode", inventoryTypeCode);
        params.put("isNotNeedOk", isNotNeedOk);
        params.put("workShopId", workShopId);
        params.put("skuCode", skuCode);
        params.put("productionPlanCode", productionPlanCode);
        params.put("productionRecordCode", productionRecordCode);
        params.put("flagStatus", flagStatus);
        params.put("startCreateTime", startCreateTime);
        params.put("endCreateTime", endCreateTime);
        if(StringUtils.isNotBlank(inventoryTypeCode)){
        }else{
            //0 领料出库 1其他出库
            params.put("operateType", "0");
        }
        params.put("status", status);
    /*    params.put("operateType", operateType);*/

        String databaseUser = DynamicDataSource.USER_PREFIX;
        if (StringUtils.isNotBlank(DynamicDataSource.name.get())) {
            databaseUser = databaseUser + "-" + DynamicDataSource.name.get();
        }
        params.put("databaseUser", databaseUser);
        IPage<ReceiptRecord> list = receiptRecordService.list(pg, params);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }


    @ApiOperation(value = "添加领料记录", notes = "添加")
    @PostMapping("/add")
    public JsonResult add(@RequestBody ReceiptRecord receiptRecord) {
        try {
            receiptRecord.setOperateType("0");
            receiptRecordService.add(receiptRecord);
        } catch (BusinessException e) {
            return JsonResult.fail(e.getMessage());
        }
        return JsonResult.success("添加成功");
    }

    @ApiOperation(value = "更新领料记录", notes = "更新")
    @PostMapping("/update")
    public JsonResult upadte(ReceiptRecord receiptRecord) {
        receiptRecordService.update(receiptRecord);
        return JsonResult.success("更新成功");
    }


    @ApiOperation(value = "审核领料记录", notes = "审核")
    @PutMapping("/audit/{receiptId}/{status}")
    public JsonResult audit(
            @PathVariable(value = "receiptId") String receiptId,
            @PathVariable(value = "status") String status
    ) {
        boolean isOk = receiptRecordService.audit(receiptId,status);
        if (isOk) {
            return JsonResult.success("更新成功");
        }else{
            return JsonResult.fail("审核失败");
        }
    }

    @ApiOperation(value = "领料记录详情", notes = "详情")
    @GetMapping("/get")
    public JsonResult get(@RequestParam(name = "id") Long id) {
        if(id==null){
            return JsonResult.fail("详情获取失败");
        }
        Map<String, Object> params = new HashMap<String, Object>();
        String databaseUser = DynamicDataSource.USER_PREFIX;
        if (StringUtils.isNotBlank(DynamicDataSource.name.get())) {
            databaseUser = databaseUser + "-" + DynamicDataSource.name.get();
        }
        params.put("databaseUser", databaseUser);
        params.put("id", id);
        return JsonResult.success(receiptRecordService.get(params));
    }

    @ApiOperation(value = "添加领料记录", notes = "添加")
    @PostMapping("/save")
    public JsonResult<ReceiptRecord> save(@RequestBody ReceiptRecord receiptRecord) {
        try {
            receiptRecord.setOperateType("0");
            receiptRecordService.save(receiptRecord);
        } catch (BusinessException e) {
            return JsonResult.fail(e.getMessage());
        }
        return JsonResult.success("添加成功", receiptRecord);
    }
    
}
